public class LockDemo implements Runnable {
    private static Object lock = new Object(); // 定义一个锁对象
    private static int count = 0; // 定义一个计数器
    public LockDemo(int id) {

    }
    public void run() {
        synchronized (lock) { // 使用锁对象进行同步
            count++; // 计数器加1
            System.out.println(Thread.currentThread().getName() + " got the lock, count = " + count);
//            try {
//                Thread.sleep(1000); // 模拟线程执行过程
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }

    public static void main(String[] args) {
        LockDemo demo = new LockDemo(0);
        for (int i = 0; i < 5; i++) { // 启动5个线程
            new Thread(demo, "Thread " + i).start();
        }

        LockDemo fairDemo = new LockDemo(0);
        for (int i = 0; i < 5; i++) { // 启动5个线程
            new Thread(fairDemo, "Fair Thread " + i).start();
        }
    }
}
